//注册表单的非空校验
function checkForm() {
    var username = document.getElementById("username").value;

    if (username == null || username == "") {
        alert("用户名不能为空");
        return false;
    }


    var password = document.getElementById("password").value;
    if (password == null || password == "") {
        alert("密码不能为空");
        return false;
    }


    var repassword = document.getElementById("repassword").value;
    if (repassword == null || repassword == "") {
        alert("确认密码不能为空");
        return false;
    }
    if (repassword != password) {
        alert("两次密码输入不一致");
        return false;
    }

    var email = document.getElementById("email").value;
    if (email == null || email == "") {
        alert("邮箱不能为空");
        return false;
    }
    var phone = document.getElementById("phone").value;
    if (phone == null || phone == "") {
        alert("电话不能为空");
        return false;
    }
    var validatecode = document.getElementById("validatecode").value;
    if (validatecode == null || validatecode == "") {
        alert("验证码不能为空");
        return false;
    }
}

// ajax校验用户名是否存在
/*function checkUsername() {
    var username = document.getElementById("username").value;// 获取用户名
    // 1.获取异步交互对象
    var xmlhttp = createXMLHttpRequest();
    // 2.设置监听
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("u").innerHTML = xmlhttp.responseText;// 显示从数据库中查找的信息
        }
    };
    // 3.打开连接(为了避免浏览器的缓存，需要加一个时间参数)
    xmlhttp.open("GET","${pageContext.request.contextPath}/user_findByUsername?a="+ new Date().getTime()+"&username="+username+"", true);
    // 4.发送请求
    xmlhttp.send(null);

}*/
function createXMLHttpRequest() {
    var xmlHttp;
    try{
        xmlHttp = new XMLHttpRequest();
    }catch(e){
        try{
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
function checkUsername() {
    //创建对象
    var xmlHTTP = createXMLHttpRequest();
    var username = document.getElementById("username").value;
    //打开
    //xmlHTTP.open("Get","/ajax/findUsernameServlet?username="+username&"time="+new Date(),true);  get方法
    xmlHTTP.open("POST", "/shop/ajaxServlet", true);//post方法
    //设置类型（post方法一定要设置）
    xmlHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //发送
    xmlHTTP.send("username=" + username + "&time=" + new Date());//post方法
    //xmlHTTP.send(null);  get方法
    //设置监听
    xmlHTTP.onreadystatechange = function () {
        var msg = document.getElementById("u");
        //xmlHttp 发送结束  且  服务器响应  200
        if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) {
            var flag = xmlHTTP.responseText;//接收的内容，以响应文本的方式接收
            if (flag == "true") {
                //alert("true");
                msg.innerText = "用户名已存在";
                msg.style.color = "red";

            } else {
                //alert("false");
                msg.innerText = "用户可以注册";
                msg.style.color = "green";
                if(username.length<3 || username.length>12){
                    msg.innerText="用户名长度在3-12之间";
                    msg.style.color="blue";
                }
            }
        }
    }

    // 创建XMLHttpRequest对象
    function createXMLHttpRequest() {
        try {
            return new XMLHttpRequest();
        } catch (e) {
            try {
                return new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("你用什么浏览器呢？");
                    throw e;
                }
            }
        }
    };
    //切换验证码
    function changecode() {
        var img = document.getElementById("validateimage");
        img.src = "${pageContext.request.contextPath}/codeaction?a=" + new Date().getTime();
    }

}
function checkPassword() {
    var password = document.getElementById("password").value;
    var msg = document.getElementById("msg");
    if(password.length<6||password.length>12){
        msg.innerText="密码长度在6-12之间";
        msg.style.color="red";
    }else{
        msg.innerText="密码可以使用";
        msg.style.color="green";
    }

}
function checkRepassword() {
    var repassword = document.getElementById("repassword").value;
    var password = document.getElementById("password").value;
    var msg = document.getElementById("r");
    if(password!=repassword){
        msg.innerText="密码不同，请确认密码";
        msg.style.color="red";
    }else{
        msg.innerText="OK";
        msg.style.color="green";
    }

}
function  checkPhone() {
    var phoneNumber = document.getElementById("phone").value;
    var msg = document.getElementById("phonemsg");
    var reg=/1[3578][0-9]{9}/;
    var flag = reg.test(phoneNumber);
    if(flag == false){
        msg.innerText="手机号码格式错误";
        msg.style.color="red";
    }else{
        msg.innerText="OK!go on";
        msg.style.color="green";
    }
}
function checkEmail() {
    var eMail = document.getElementById("email").value;
    var msg = document.getElementById("e");
    var reg = /[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9-]+)+/;
    var flag = reg.test(eMail);
    if(flag == false){
        msg.innerText="Email格式错误";
        msg.style.color="red";
    }else{
        msg.innerText="OK!go on";
        msg.style.color="green";
    }
}
	
	
	
	
	
	
	
	